﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChainAnalises.Classes.IntervalAnalysis.Characteristics;
using MarkovCompare.Excel;
using Segmentator.Interfaces;

namespace Segmentator.Dividers
{
    /// <summary>
    /// Организует вывод данных в Excel файл.
    /// </summary>
    class OutExcelDevice:Device
    {
        private ExcelDevice Excel;
        
        public OutExcelDevice(String name)
        {
            Name = name;
        }

        public OutExcelDevice(IDataLoader dataLoader)
        {
            Name = FormFileName(dataLoader);
        }


        public override void Print(BaseCharacteristic results, IDataUploader inputData)
        {   
  
            List<string> charact;
            List<string> alph;
            List<string> count;
            List<string> name;
            List<string> value;
            List<string> keyNameList = GetKeyNameList(results, out charact, out alph, out count, out name, out value);
            
            ExcelDevice excel = new ExcelDevice();
            excel.NewDocument();
            excel.SetColunOfValues(keyNameList, "Характеристика", 1);
            excel.SetColunOfValues(charact, "Значение", 2);
            excel.SetColunOfValues(alph, "Словарь склеек", 4);
            excel.SetColunOfValues(count, "Число вхождений", 5);
            excel.SetColunOfValues(name, "Входные данные", 6);
            excel.SetColunOfValues(value, "Значение", 7);

            if (File.Exists(Name)) File.Delete(Name);
            excel.SaveDocument(Name);
            excel.CloseDocument();
            
        }

        private List<string> GetKeyNameList(BaseCharacteristic results, out List<string> charact, out List<string> alph, out List<string> count, out List<string> name, out List<string> value)
        {
            List<string> keyNameList = new List<string>();
            foreach (string keyName in results.GetCaracteristic().Keys) keyNameList.Add(keyName);

            charact = new List<string>();
            foreach (double valueName in results.GetCaracteristic().Values) charact.Add(valueName.ToString());

            alph = new List<string>();
            foreach (string keyName in results.GetDictionary().Keys) alph.Add(keyName);

            count = new List<string>();
            foreach (int valueName in results.GetDictionary().Values) count.Add(valueName.ToString());

            name = new List<string>();
            foreach (string keyName in results.GetInputCharacteristic().Keys) name.Add(keyName);

            value = new List<string>();
            foreach (string valueName in results.GetInputCharacteristic().Values) value.Add(valueName.ToString());
            return keyNameList;
        }

        /// <summary>
        /// Фирмирует название файла из входных параметров сегментирования
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        private string FormFileName(IDataLoader inputData)
        {
            return Application.StartupPath + "\\" + inputData.GetChainName() + "_Length" +
                   inputData.GetLengthStartWindow() + "_Limit" + inputData.GetThresholdLeft() + "-" + inputData.GetThresholdRight() +
                   "_Starts" + inputData.GetAmountStartAlgorithm() + "_Balance" + inputData.GetBalanceFactor() + ".xls";
        }
    }
}
